package com.jiandaoshou;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jiandaoshou.base.BaseActivity;
import com.jiandaoshou.http.APIDefine;
import com.jiandaoshou.http.Http;
import com.jiandaoshou.utils.ShareUtil;
import com.jiandaoshou.utils.ToastUtil;
import com.jiandaoshou.utils.UserUtils;

import java.util.HashMap;

import cn.smssdk.EventHandler;
import cn.smssdk.OnSendMessageHandler;
import cn.smssdk.SMSSDK;

public class RePassWordAct extends BaseActivity implements View.OnClickListener {
    private EditText Edit_PhoneNum;
    private EditText Edit_PassWord;
    private EditText Edit_rePassWord;
    private String Str_PhoneNum;
    private String Str_PassWord;
    private String Str_RePassWord;
    private Button Btn_Submit;
    private String ShopID = "";

    private TextView tv_getcode;
    private EditText edt_code;

    @Override
    public boolean showBackLogo() {
        return true;
    }

    @Override
    protected int getContentViewRes() {
        return R.layout.activity_repassword;
    }

    @Override
    protected String getTitleText() {
        return "修改密码";
    }

    @Override
    public void init() {
        Edit_PhoneNum = $(R.id.edit_phonenumber);
        Edit_PassWord = $(R.id.edit_password);
        Edit_rePassWord = $(R.id.edit_repassword);
        Btn_Submit = $(R.id.btn_submit);
        tv_getcode = $(R.id.tv_getcode);
        edt_code = $(R.id.edt_code);
        Btn_Submit.setOnClickListener(this);
        tv_getcode.setOnClickListener(this);
        ShopID = getIntent().getStringExtra("shopId");

        Edit_PhoneNum.setText(UserUtils.getShopPhone(this));
    }

    private int time = 60;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    cn.smssdk.SMSSDK.registerEventHandler(eh);
                    ToastUtil.toast("验证码发送成功");
                    Message me = Message.obtain();
                    time --;
                    me.what = 3;
                    sendMessageDelayed(me, 1000);
                    break;
                case 2:
                    Str_PassWord = Edit_PassWord.getText().toString();
                    Str_RePassWord = Edit_rePassWord.getText().toString();
                    Str_PhoneNum = Edit_PhoneNum.getText().toString();
                    if (!Str_PassWord.equals(Str_RePassWord)) {
                        Toast.makeText(RePassWordAct.this, "密码不一致！", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (Str_PhoneNum == null || Str_PhoneNum.equals("")) {
                        return;
                    }
                    if (Str_PassWord == null || Str_PassWord.equals("")) {
                        return;
                    }
                    String Str_oldPassWord = ShareUtil.getInstance().GetPassWord(RePassWordAct.this);
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("strNewPassword", Str_PassWord);
                    map.put("strOldPassword", Str_oldPassWord);
                    map.put("strShopID", ShopID);
                    Http.post(APIDefine.TF_Interface_UpdatePassWord, map, new Http.HttpCallBack() {
                        @Override
                        public void onHttpSuccess(String response) throws Exception {
                            Toast.makeText(RePassWordAct.this, "修改成功!", Toast.LENGTH_SHORT).show();
                            RePassWordAct.this.finish();
                        }
                    });
                    break;
                case 3:
                    if (time <= 0) {
                        tv_getcode.setEnabled(true);
                        tv_getcode.setText("获取验证码");
                    } else {
                        tv_getcode.setEnabled(false);
                        tv_getcode.setText(String.valueOf(time));
                    }
                    if(time >= 0){
                        time --;
                        Message me1 = Message.obtain();
                        me1.what = 3;
                        sendMessageDelayed(me1, 1000);
                    }
                    break;
            }
        }
    };

    private EventHandler eh = new EventHandler() {
        @Override
        public void onRegister() {
            super.onRegister();
        }

        @Override
        public void beforeEvent(int i, Object o) {
            super.beforeEvent(i, o);
        }

        @Override
        public void afterEvent(int event, int result, Object data) {
            super.afterEvent(event, result, data);
                //回调完成
                if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                    //提交验证码成功
                    handler.sendEmptyMessage(2);
                } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                    //获取验证码成功
                } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
                    //返回支持发送验证码的国家列表
                }
        }

        @Override
        public void onUnregister() {
            super.onUnregister();
        }
    };

    @Override
    public void onClick(View view) {
        final String phone = Edit_PhoneNum.getText().toString().trim();
        switch (view.getId()) {
            case R.id.tv_getcode:
                if (TextUtils.isEmpty(phone) == false && phone.length() == 11 && phone.startsWith("1")) {
                    cn.smssdk.SMSSDK.getVerificationCode("86", phone, new OnSendMessageHandler() {
                        @Override
                        public boolean onSendMessage(String s, String s1) {
                            handler.sendEmptyMessage(1);
                            return false;
                        }
                    });
                } else {
                    Toast.makeText(RePassWordAct.this, "请输入正确的手机号码！", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_submit:
                String code = edt_code.getText().toString().trim();
                String pwd=Edit_PassWord.getText().toString();
                String pwd2=Edit_rePassWord.getText().toString();

                if (TextUtils.isEmpty(phone)) {
                    Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (TextUtils.isEmpty(code)) {
                    Toast.makeText(this, "请输入验证码", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (TextUtils.isEmpty(pwd)) {
                    Toast.makeText(this, "请输入新密码", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (TextUtils.isEmpty(pwd2)) {
                    Toast.makeText(this, "请输入确认密码", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!pwd.equals(pwd2)) {
                    Toast.makeText(this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }

                cn.smssdk.SMSSDK.submitVerificationCode("86", phone, code);
                break;
        }
    }
}
